import { Body, JsonController, Post, Get, Param, NotFoundError, Put, QueryParam, QueryParams, Ctx } from 'routing-controllers'
import { Inject, Service } from 'typedi'
import { UserService } from '@/services/UserService'
import { CreateUserDto, UpdateUserDto, UserDto } from '@/dtos/UserDto';
import { BasePageRequestDto, BasePageResponseDto, BaseSingleResponseDto } from '@/dtos/BaseDto';
import { Context } from 'koa';

@Service()
@JsonController('/users')
export class UserController {

  @Inject()
  private userService!: UserService;

  @Get('/')
  public async getUsers(@QueryParams() params: BasePageRequestDto, @Ctx() ctx: Context): Promise<BasePageResponseDto<UserDto>> {
    return this.userService.findAll(params);
  }

  @Get('/:id')
  public async getUserById(@Param('id') userId: number): Promise<BaseSingleResponseDto<UserDto>> {
    return this.userService.get(userId);
  }

  @Post('/')
  public async createUser(@Body() user: CreateUserDto): Promise<BaseSingleResponseDto<UserDto>> {
    return this.userService.create(user);
  }

  @Put('/')
  public async updateUser(@Body() user: UpdateUserDto): Promise<BaseSingleResponseDto<UserDto>> {
    return this.userService.update(user);
  }

  @Post('/batch-delete')
  public async batchDelete(@Body() userIds: Array<number>): Promise<BaseSingleResponseDto<boolean>> {
    return this.userService.batchDelete(userIds);
  }

}
